package com.work.csms.mapper;

import com.work.csms.dao.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM USER")
    @Results({
            @Result(property = "userid", column = "userid"),
            @Result(property = "username", column = "username"),
            @Result(property = "name", column = "name"),
            @Result(property = "password", column = "password"),
            @Result(property = "telephone", column = "telephone"),
            @Result(property = "email", column = "email"),
            @Result(property = "usertype", column = "usertype"),
            @Result(property = "money", column = "money"),
            @Result(property = "date", column = "date")})
    List<User> queryAllUser();

    @Select({"<script>" +
            "SELECT * FROM USER " +
            "where userType = 1" +
            "<if test=\"name != null and name != '' \"> " +
            " AND name like concat('%',#{name},'%')" +
            "</if>" +
            " limit #{offset} , #{size}" +
            "</script>"})
    @Results({
            @Result(property = "userid", column = "userid"),
            @Result(property = "username", column = "username"),
            @Result(property = "name", column = "name"),
            @Result(property = "password", column = "password"),
            @Result(property = "telephone", column = "telephone"),
            @Result(property = "email", column = "email"),
            @Result(property = "usertype", column = "usertype"),
            @Result(property = "money", column = "money"),
            @Result(property = "date", column = "date")})
    List<User> page(String name, Integer offset, Integer size);

    @Select({"<script>" +
            "SELECT count(0) FROM USER " +
            "where userType = 1" +
            "<if test=\"name != null and name != '' \"> " +
            " AND name like concat('%',#{name},'%')" +
            "</if>" +
            "</script>"})
    Integer count(String name);

    @Select("SELECT * FROM USER WHERE USERID=#{userid}")
    @Results({
            @Result(property = "userid", column = "userid"),
            @Result(property = "username", column = "username"),
            @Result(property = "name", column = "name"),
            @Result(property = "password", column = "password"),
            @Result(property = "telephone", column = "telephone"),
            @Result(property = "email", column = "email"),
            @Result(property = "usertype", column = "usertype"),
            @Result(property = "money", column = "money"),
            @Result(property = "date", column = "date")})
    User getUserById(Integer userid);

    @Select("SELECT * FROM USER WHERE username=#{username}")
    User getUserByUserName(String username);

    @Select("SELECT USERID FROM USER WHERE USERNAME=#{username} AND PASSWORD=#{password}")
    @Results({
            @Result(property = "userid", column = "userid"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password") })
    Integer login(String username, String password);

    @Select("SELECT COUNT(*) FROM USER WHERE USERNAME=#{username}")
    Integer hasUsername(String username);

    @Insert("INSERT INTO USER(USERNAME, PASSWORD, USERTYPE) VALUES (#{username}, #{password}, #{usertype})")
    @Results({
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "usertype", column = "usertype")})
    void regist(String username, String password, Integer usertype);

    @Insert("INSERT INTO USER(username, name, telephone, usertype) VALUES (#{username}, #{name}, #{telephone}, #{usertype})")
    void regist2(String username, String name, String telephone, Integer usertype);

    @Update("UPDATE USER SET USERNAME=#{username}, NAME=#{name}, PASSWORD=#{password}, " +
            "TELEPHONE=#{telephone}, EMAIL=#{email} WHERE USERID=#{userid}")
    @Results({
            @Result(property = "userid", column = "userid"),
            @Result(property = "username", column = "username"),
            @Result(property = "name", column = "name"),
            @Result(property = "password", column = "password"),
            @Result(property = "telephone", column = "telephone"),
            @Result(property = "email", column = "email")})
    void updateUser(Integer userid, String username, String name, String password, String telephone,
                    String email);

    @Update("UPDATE USER SET money= money + #{money} WHERE USERID=#{userid}")
    @Results({
            @Result(property = "userid", column = "userid"),
            @Result(property = "money", column = "money")})
    void updateMoneyByUserid(Integer userid, Integer money);

    @Insert("INSERT INTO USER(USERNAME, NAME, PASSWORD, USERTYPE, TELEPHONE, EMAIL) VALUES " +
            "(#{user.username}, #{user.name}, #{user.password}, #{user.usertype}, #{user.telephone}, #{user.email})")
    void save(@Param("user")User user);

    @Delete("delete from USER where userid = #{userid} ")
    void deleted(Integer userid);
}
